package com.qfedu.mtl.controller;


import com.alibaba.fastjson.JSON;
import com.qfedu.mtl.entity.Webuser;
import com.qfedu.mtl.query.WebUserQuery;
import com.qfedu.mtl.service.WebUserService;
import com.qfedu.mtl.service.impl.WebUserServiceImpl;
import com.qfedu.mtl.util.CharacterUtil;
import com.qfedu.mtl.util.HttpUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.page.Pager;
import com.qfedu.mtl.util.web.annotation.Controller;
import com.qfedu.mtl.util.web.annotation.RequestMapping;
import com.qfedu.mtl.vo.TableVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Controller
@RequestMapping("/webUser/")
public class WebUserController {
    WebUserService webSer = new WebUserServiceImpl();
    private UserController webUserService;

    //添加用户
    @RequestMapping("add.do")
    public void addWebUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setUtf8(req, resp);
        Webuser webuser = HttpUtil.request2Bean(req, Webuser.class);

        try {
            webSer.add(webuser);
            HttpUtil.response(null, 0, "成功", resp);

        } catch (Exception ex) {
            HttpUtil.response(null, -1, "失败" + ex.getMessage(), resp);
        }

    }

    ;

    //删除
    @RequestMapping("delete.do")
    public void deleteWebUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setUtf8(req, resp);

        String id = req.getParameter("ids");
        String[] ids = id.split(",");
        try {
            //如果前端传来的id长度不止1
            if (ids.length > 1) {
                //传回多个id
                for (String s : ids) {
                    webSer.delete(Integer.valueOf(s));
                }
            } else {
                //传回单个id
                webSer.delete(Integer.valueOf(id));

            }
            HttpUtil.response(null, 0, "成功", resp);
        } catch (Exception ex) {
            HttpUtil.response(null, -1, "失败" + ex.getMessage(), resp);
        }
    }

    //更新信息
    @RequestMapping("update.do")
    public void updateWebUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setUtf8(req, resp);
        Webuser webuser = HttpUtil.request2Bean(req, Webuser.class);
        System.out.println(webuser.getUid());
        try {
            webSer.update(webuser);
            HttpUtil.response(null, 0, "成功", resp);
        } catch (Exception ex) {
            HttpUtil.response(null, -1, "失败" + ex.getMessage(), resp);

        }

    }

    ;

    //通过id查询
    @RequestMapping("find.do")
    public void findWebUserById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setUtf8(req, resp);

        String id = req.getParameter("uid");
        try {
            Webuser webuser = webSer.findById(Integer.valueOf(id));
            HttpUtil.response(webuser, 0, "成功", resp);

        } catch (Exception ex) {
            HttpUtil.response(null, -1, "失败" + ex.getMessage(), resp);

        }

    }

    ;

    //    分页条件查询
    @RequestMapping("findAll.do")
    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setUtf8(req, resp);

        String pag = req.getParameter("page");
        String siz = req.getParameter("size");
        try {
            WebUserQuery webUserQuery = HttpUtil.request2Bean(req, WebUserQuery.class);


            Integer page = 1;
            Integer size = 10;
            //判断页码和每页数量是否为空
            if (!StrUtil.isNullOrEmpty(pag)) {
                page = Integer.valueOf(pag);
            }
            if (!StrUtil.isNullOrEmpty(siz)) {
                size = Integer.valueOf(siz);
            }
            Pager allPage = webSer.findAllPage(page, size, webUserQuery);
            TableVO vo = TableVO.from(allPage);
            String json = JSON.toJSONString(vo);
            resp.getWriter().write(json);


        } catch (Exception ex) {
            HttpUtil.response(null, -1, "失败" + ex.getMessage(), resp);

        }

    }
}
